package com.springboot.controller;

import com.springboot.dto.MessageView;
import com.springboot.model.MessageInform;
import com.springboot.model.UserLoginForm;
import com.springboot.service.PatientMyInformService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * @ description:患者端我的通知控制台响应
 * @ date       :2020/12/22
 * @        :张峻宇
*/
@Controller
public class PatientMyInformController {
    @Autowired
    UserLoginService userLoginService;

    @Autowired
    PatientMyInformService patientMyInformService;

    /**
     * @ description:在“我的通知”给患者展示今天或者未读的通知
     * @ param     :[request, unitphone]
     * @ return    :java.lang.String
     * @ date       :2020/12/22

    */
    @CrossOrigin
    @RequestMapping(value = "/patientshowmessage")
    @ResponseBody
    public ArrayList<MessageView> patientShowMessage(HttpServletRequest request , @RequestParam("unitphone") String tel)  {
        UserLoginForm userLoginForm = userLoginService.findUserByTel(tel);
        Integer patientId = userLoginForm.getUserId();
        ArrayList<MessageView> MessageViews = patientMyInformService.showMessageByPatientId(patientId);
        return MessageViews;
    }

    /**
     * @ description:患者阅读通知，把患者信息状态改为已读
     * @ param      :[request, messageId]
     * @ return     :java.lang.String
     * @ date       :2020/12/22

    */
    @CrossOrigin
    @RequestMapping(value = "/patientreadmessage")
    @ResponseBody
    public String patientReadMessage(HttpServletRequest request , @RequestParam("messageId") Integer messageId)  {
       if (patientMyInformService.updateMessageByMessageId(messageId)==1)
       {
           return "True";
       }
       else {
           return "False";
       }
    }

    /*
     * @ description:患者点击一键已读时，把患者消息全设置为已读
     * @ param      :[request, tel] 患者手机号
     * @ return     :java.lang.String
     * @ date       :2020/12/31

    */
    @CrossOrigin
    @RequestMapping(value = "/patientreadallmessage")
    @ResponseBody
    public ArrayList<MessageView> patientReadAllMessage(HttpServletRequest request , @RequestParam("unitphone") String tel)  {
        UserLoginForm userLoginForm = userLoginService.findUserByTel(tel);
        patientMyInformService.readAllMessage(userLoginForm.getUserId());
        System.out.println("一键已读");
        ArrayList<MessageView> MessageViews = patientMyInformService.showMessageByPatientId(userLoginForm.getUserId());
        return MessageViews;
    }

    /**
     * @ description:患者点击“我的”时，向前端发送未读通知数量和用户头像
     * @ param      :[tel] 用户手机号
     * @ return     :java.lang.String
     * @ date       :2021/1/2

    */
    @CrossOrigin
    @RequestMapping(value = "/patientshowmytable")
    @ResponseBody
    public String patientShowMyTable(@RequestParam("unitphone") String tel)
    {
        UserLoginForm userLoginForm = userLoginService.findUserByTel(tel);
        int num = patientMyInformService.findUnreadNum(userLoginForm.getUserId());
        String result = "{\"logolink\":"+"\""+userLoginForm.getLogoLink()+"\"" +","+"\"num\":"+num+"}";
        return result;
    }
}
